import { getDepartmentSelector } from '@/api/permission/department'
import request from '@/utils/request'
import CustomForm from './Form/index.vue'
export default {
  name: 'lecturer',
  components: { CustomForm },
  data() {
    return {
      showAll: false,
      query: {
        name: undefined,
        departmentId: undefined
      },
      list: [],
      listLoading: true,

      departmentTreeData: [],

      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      },
      formVisible: false,
      genderOptions: [],

      categoryOptions: [
        {
          enCode: '内部讲师',
          fullName: '内部讲师'
        },
        {
          enCode: '特聘讲师',
          fullName: '特聘讲师'
        }
      ]
    }
  },
  computed: {},
  created() {
    this.initData()
  },
  mounted() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'sex' }).then(res => {
      this.genderOptions = JSON.parse(JSON.stringify(res))
    })
    getDepartmentSelector().then(res => {
      this.departmentTreeData = res.data.list
    })
  },
  methods: {
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/train/course`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list.map(item => {
          item.photoPath = item.photoPath ? JSON.parse(item.photoPath) : []
          return item
        })
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/train/course/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(row, information = false) {
      let data = Object.assign({}, row)
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(data, information)
      })
    },

    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    }
  }
}
